package cn.lixuan.controller;

import cn.lixuan.bo.VlogBO;
import cn.lixuan.constants.BaseInfoProperties;
import cn.lixuan.enums.YesOrNo;
import cn.lixuan.grace.result.GraceJSONResult;
import cn.lixuan.service.VlogService;
import cn.lixuan.utils.PagedGridResult;
import cn.lixuan.vo.IndexVlogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "短视频相关接口")
@RequestMapping("vlog")
public class VlogController extends BaseInfoProperties {

    @Autowired
    private VlogService vlogService;

    @ApiOperation(value = "发布短视频功能")
    @PostMapping("publish")
    public GraceJSONResult publish(@RequestBody VlogBO vlogBO) {

        vlogService.createVlog(vlogBO);

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "获取视频详情")
    @GetMapping("detail")
    public GraceJSONResult detail(@RequestParam(defaultValue = "") String userId,
                                  @RequestParam String vlogId) {

        IndexVlogVO vlogVO = vlogService.getVlogDetailById(userId, vlogId);

        return GraceJSONResult.ok(vlogVO);
    }

    @ApiOperation(value = "获取首页视频推荐功能")
    @GetMapping("indexList")
    public GraceJSONResult indexList(@RequestParam(defaultValue = "") String userId,
                                     @RequestParam(defaultValue = "") String search,
                                     @RequestParam Integer page,
                                     @RequestParam Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = vlogService.getIndexVlogList(userId, search, page, pageSize);
        return GraceJSONResult.ok(gridResult);
    }

    @ApiOperation(value = "将个人发布的视频设置为私密")
    @PostMapping("changeToPrivate")
    public GraceJSONResult changeToPrivate(@RequestParam String userId, @RequestParam String vlogId) {

        vlogService.changeToPrivateOrPublish(userId, vlogId, YesOrNo.YES.type);
        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "将个人发布的视频设置为公开")
    @PostMapping("changeToPublic")
    public GraceJSONResult changeToPublic(@RequestParam String userId, @RequestParam String vlogId) {

        vlogService.changeToPrivateOrPublish(userId, vlogId, YesOrNo.NO.type);
        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "我公开的视频")
    @GetMapping("myPublicList")
    public GraceJSONResult myPublicList(@RequestParam String userId,
                                        @RequestParam Integer page,
                                        @RequestParam Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = vlogService.queryMyVlogList(userId, page, pageSize, YesOrNo.NO.type);
        return GraceJSONResult.ok(gridResult);
    }

    @ApiOperation(value = "我私密的视频")
    @GetMapping("myPrivateList")
    public GraceJSONResult myPrivateList(@RequestParam String userId,
                                         @RequestParam Integer page,
                                         @RequestParam Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = vlogService.queryMyVlogList(userId, page, pageSize, YesOrNo.YES.type);
        return GraceJSONResult.ok(gridResult);
    }

    @ApiOperation(value = "我点赞的视频")
    @GetMapping("myLikedList")
    public GraceJSONResult myLikedList(@RequestParam String userId,
                                         @RequestParam Integer page,
                                         @RequestParam Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = vlogService.getMyLikedVlogList(userId, page, pageSize);
        return GraceJSONResult.ok(gridResult);
    }

    @ApiOperation(value = "用户点赞/喜欢")
    @PostMapping("like")
    public GraceJSONResult like(@RequestParam String userId,
                                @RequestParam String vlogerId,
                                @RequestParam String vlogId) {

        vlogService.userLikeVlog(userId, vlogId);

        // 点赞后，视频和视频发布者的获赞都会+1
        redis.increment(BaseInfoProperties.REDIS_VLOG_BE_LIKED_COUNTS + vlogerId, 1);
        redis.increment(BaseInfoProperties.REDIS_VLOGER_BE_LIKED_COUNTS + vlogId, 1);
        // 我点赞的视频，需要在redis中保存关联关系
        redis.set(BaseInfoProperties.REDIS_USER_LIKE_VLOG + userId +":"+vlogId, "1");

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "用户取消点赞/喜欢")
    @PostMapping("unlike")
    public GraceJSONResult unlike(@RequestParam String userId,
                                @RequestParam String vlogerId,
                                @RequestParam String vlogId) {

        vlogService.userUnLikeVlog(userId, vlogId);

        redis.decrement(BaseInfoProperties.REDIS_VLOG_BE_LIKED_COUNTS + vlogerId, 1);
        redis.decrement(BaseInfoProperties.REDIS_VLOGER_BE_LIKED_COUNTS + vlogId, 1);
        // 我点赞的视频，需要在redis中保存关联关系
        redis.del(BaseInfoProperties.REDIS_USER_LIKE_VLOG + userId +":"+vlogId);

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "获取视频点赞/喜欢总数")
    @PostMapping("totalLikedCounts")
    public GraceJSONResult totalLikedCounts(@RequestParam String vlogId) {
        return GraceJSONResult.ok(vlogService.getVlogBeLikedCounts(vlogId));
    }

    @ApiOperation(value = "获取用户关注博主发布的短视频")
    @GetMapping("followList")
    public GraceJSONResult followList(@RequestParam String myId, @RequestParam Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult list = vlogService.getMyFollowVlogList(myId, page, pageSize);

        return GraceJSONResult.ok(list);
    }

    @ApiOperation(value = "获取朋友发布的短视频")
    @GetMapping("friendList")
    public GraceJSONResult friendList(@RequestParam String myId, @RequestParam Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult list = vlogService.getMyFriendVlogList(myId, page, pageSize);

        return GraceJSONResult.ok(list);
    }
}
